package com.example.omegainventoryservice;

import com.example.omegainventoryservice.dao.InventoryDao;
import com.example.omegainventoryservice.domain.Inventory;
import org.apache.servicecomb.saga.omega.spring.EnableOmega;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Objects;

@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.omegainventoryservice.dao")
@EnableTransactionManagement
@EnableJpaAuditing
@EnableOmega
public class OmegaInventoryServiceApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(OmegaInventoryServiceApplication.class, args);
    }

    @Autowired
    InventoryDao inventoryDao;

    @Override
    public void run(String... args) throws Exception {
        String productCode = "spring-cloud-in-action";
//        Inventory inventory = Inventory.builder()
//                .leftNum(100)
//                .productCode("spring-cloud-in-action")
//                .build();
        Inventory inventory = inventoryDao.findByProductCode(productCode);
        if (Objects.isNull(inventory)) {
            inventory = new Inventory();
            inventory.setLeftNum(100);
            inventory.setProductCode(productCode);
            inventoryDao.save(inventory);
        }
    }
}
